package com.ruoyi.common.utils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 工具类
 * @author zj
 *
 */
public class ToolUtil {
	public static String fmtstr(String strr)
	{
		String str = "";
		if(strr!=null)
		{
			str = strr.trim();
		}
		return str;
	}
	
	public static String fmtstr(Long strr)
	{
		String str = "";
		if(strr!=null)
		{
			str = strr+"";
		}
		return str;
	}
	
	public static String fmtstr(Integer strr)
	{
		String str = "";
		if(strr!=null)
		{
			str = strr+"";
		}
		return str;
	}
	
	public static String fmtstr(Double strr)
	{
		String str = "";
		if(strr!=null)
		{
			str = strr+"";
		}
		return str;
	}
	
	public static String fmtStr(String tstrr) {
		if (tstrr == null || tstrr.equals("null")) {
			return "";
		} else {

			return tstrr.trim();
		}
	}

	/**
	 * 乘法
	 * @param v1
	 * @param v2
	 * @return
	 */
	public static double mul(double v1,double v2){     
		BigDecimal b1 = new BigDecimal(Double.toString(v1));     
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b1.multiply(b2).doubleValue();     
	}
	/**
	 * 减法
	 * @param v1
	 * @param v2
	 * @return
	 */
	public static double sub(double v1,double v2){     
		BigDecimal b1 = new BigDecimal(Double.toString(v1));     
		BigDecimal b2 = new BigDecimal(Double.toString(v2)); 
		return b1.subtract(b2).doubleValue();     
	}
	/**
	 * 除法
	 * @param v1 被除数,保留4位小数
	 * @param v2 chushu
	 * @return
	 */
	public static double div(double v1,double v2){     
		BigDecimal b1 = new BigDecimal(Double.toString(v1));     
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b2.divide(b1,4,BigDecimal.ROUND_HALF_UP).doubleValue();     
	}
	/**
	 * 验证身份证号码是否有效
	 */
	public static Boolean validIdCard(String targetStr){
		String reg = "[1-9]{2}[0-9]{4}(19|20)[0-9]{2}"
                + "((0[1-9]{1})|(1[1-2]{1}))((0[1-9]{1})|([1-2]{1}[0-9]{1}|(3[0-1]{1})))"
                + "[0-9]{3}[0-9x]{1}";
        return valid(reg,targetStr);
	}
	private static boolean valid(String reg,String targetStr){
		Pattern pattern = Pattern.compile(reg);
		return pattern.matcher(targetStr).matches() ? false : true;
	}

	public static   Double add(Double... values){
		Double total=0.0;
		for(Double value:values){
			BigDecimal bigDecimal1 = new BigDecimal(Double.toString(total));
			BigDecimal bigDecimal2 = new BigDecimal(Double.toString(value));
			total=bigDecimal1.add(bigDecimal2).doubleValue();
		}
		return  total;
	}
	public static   int  compareTo(Double a,Double b){
		Double obj1 = new Double(a);
		Double obj2 = new Double(b);
		return  obj1.compareTo(obj2);
	}

	public static String fmtstr(Object object) {
		if(object == null){
			return "";
		}
		return object.toString();
	}
	
	/**
     * 小时:分钟的正则表达式检查<br>
     * <br>
     * @param time     要检查的字符串<br>
     * @return boolean   返回检查结果<br>
     */
    public static boolean isTimeStr (String time) {
        if(time == null){
            return false;
        }
        String regEx = "([0-1][0-9]|2[0-3]):[0-5][0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher matcher = p.matcher(time);
        return matcher.matches();
    }
    
    public static List<String> getArrayToString(String str){
    	List<String> list = null;
    	if(str == null)
    		return list;
    	list = Arrays.asList(str.split(",", -1));
    	return list;
    }
    
    public static void main(String[] args) {
//    	String payPassword = DESUtil.encode("123456", "zhongjing");
//    	String newPassword = DESUtil.decode("1EE01CC0805A91BF", "zhongjing");
//    	System.out.println(newPassword+" --- "+newPassword);
//    	System.out.println(getTimeStr(0));
//    	System.out.println(getTimeStr(60));
		Calendar calendar = Calendar.getInstance();
		Calendar calendar1 = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_YEAR,7);
		System.out.println(calendar.getTime());
		System.out.println(calendar1.getTime());
		System.out.println(calendar.after(calendar1));
    }
    /**
     * 换成一定格式的时间字符串
     */
    public static String getTimeStr(int s){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    	Calendar c = Calendar.getInstance();
    	c.set(Calendar.SECOND, (int)(Calendar.SECOND+s));
    	return sdf.format(c.getTime());
    }
    /**
     * 给支付宝发订单时 使用orderNo+"id"+organiseTypeId字符串
     */
    public static String getOrderNo(String orderNo,Long organiseTypeId){
    	if(orderNo==null || organiseTypeId==null){
    		return null;
    	}
    	return (orderNo+"id"+organiseTypeId);
    }
    
    public static Integer yuanToFen(String yuan) {
		return new BigDecimal(yuan).setScale(2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue();
	}
    
    public static String FenToyuan(String yuan) {
    	return new BigDecimal(yuan).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_DOWN).toString();
    }
	/**
	 *
	 * @param organiseRate 抽成的比例
	 * @param applayMoney 申请的转账的金额
	 * @return
	 */
    public static BigDecimal getTargetCount(BigDecimal organiseRate, Double applayMoney) {
    	if (organiseRate.compareTo(BigDecimal.ZERO)==0){
    		return BigDecimal.valueOf(applayMoney);
		}
    	return BigDecimal.valueOf(applayMoney).multiply((new BigDecimal(1).subtract(organiseRate)));
    }

	/**
	 * 判断一个日期是否是今天
	 * @param calendar 判断的日期
	 * @return true 是 false 不是
	 */
	public static boolean isToday(Calendar calendar){
		Date now = new Date();//获取今天的日期
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		Date time = calendar.getTime();
		String nowDay = sf.format(now);
		// 对比的时间
		String day = sf.format(time);
		return day.equals(nowDay);
	}
}
